package com.lswms.modules.lswms.service.impl.check;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.lswms.common.service.impl.CrudServiceImpl;
import com.lswms.modules.lswms.Constant.CheckConstant;
import com.lswms.modules.lswms.Constant.TaskConstant;
import com.lswms.modules.lswms.dao.check.CheckDao;
import com.lswms.modules.lswms.dao.check.CheckdetailDao;
import com.lswms.modules.lswms.dao.task.TaskDao;
import com.lswms.modules.lswms.dto.check.CheckDTO;
import com.lswms.modules.lswms.entity.check.CheckEntity;
import com.lswms.modules.lswms.entity.check.CheckdetailEntity;
import com.lswms.modules.lswms.entity.task.TaskEntity;
import com.lswms.modules.lswms.service.check.CheckService;
import com.lswms.modules.security.user.SecurityUser;
import com.mysql.fabric.xmlrpc.base.Array;

/**
 * 盘点
 *
 * @author DX sunlightcs@gmail.com
 * @since 1.0.0 2021-07-08
 */
@Service
public class CheckServiceImpl extends CrudServiceImpl<CheckDao, CheckEntity, CheckDTO> implements CheckService {

	@Autowired
	TaskDao taskDao;
	
	@Autowired
	CheckdetailDao checkdetailDao;
	
	
	@Override
	public QueryWrapper<CheckEntity> getWrapper(Map<String, Object> params) {
		String id = (String) params.get("id");

		QueryWrapper<CheckEntity> wrapper = new QueryWrapper<>();
		wrapper.eq(StringUtils.isNotBlank(id), "id", id);

		return wrapper;
	}

	@Override
	public String createcheck(CheckEntity checkEntity) {

		Map<String, Object> map = new HashMap<String, Object>();

		map.put("checkkey", checkEntity.getCheckkey());
		map.put("user", SecurityUser.getUser().getUsername());
		map.put("O_ERROR_CODE", "");
		map.put("O_ERROR_DESC", "");
		baseDao.createcheck(map);

		return (String) map.get("O_ERROR_DESC");
	}

	@Override
	public String createcheckTask(CheckEntity checkEntity) {
		Map<String, Object> map = new HashMap<String, Object>();

		map.put("checkkey", checkEntity.getCheckkey());
		map.put("user", SecurityUser.getUser().getUsername());
		map.put("O_ERROR_CODE", "");
		map.put("O_ERROR_DESC", "");
		baseDao.createcheckTask(map);

		return (String) map.get("O_ERROR_DESC");
	}

	@Override
	public String checkPost(CheckEntity checkEntity) {
		Map<String, Object> map = new HashMap<String, Object>();

		map.put("checkkey", checkEntity.getCheckkey());
		map.put("user", SecurityUser.getUser().getUsername());
		map.put("O_ERROR_CODE", "");
		map.put("O_ERROR_DESC", "");
		baseDao.checkPost(map);

		return (String) map.get("O_ERROR_DESC");
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public String cancelCheckPost(CheckEntity checkEntity) {
		String checkkey =  checkEntity.getCheckkey();
		
		//查询盘点状态
		QueryWrapper<CheckEntity> checkQuery = new QueryWrapper<CheckEntity>();
		checkQuery.eq("checkkey", checkkey) ;
		CheckEntity checkEntityQuery = baseDao.selectOne(checkQuery);
		Integer status = checkEntityQuery.getStatus();
		if(status == CheckConstant.CHECK_STATUS_9) {
			return " 盘点已过账，无法取消" ;
		}
		
		//检查盘点任务是否都已经完成
		QueryWrapper<TaskEntity> taskQuery = new QueryWrapper<TaskEntity>();
		taskQuery.eq("tasktype", TaskConstant.TASK_CC);
		taskQuery.eq("sourcekey", checkkey) ;
		taskQuery.lt("status", TaskConstant.TASK_9) ;
		Integer count = taskDao.selectCount(taskQuery) ;
		if(count>0) {
			return " 盘点任务没有完成，无法取消，请等待盘点任务完成" ;
		}
		
		//所有的盘点单都回到原库位
		QueryWrapper<CheckdetailEntity> checkdetailQuery = new QueryWrapper<CheckdetailEntity>();
		checkdetailQuery.eq("checkkey", checkkey) ;
		checkdetailQuery.isNotNull("lot");
		checkdetailQuery.notExists( " select 1 from  lswms_inventory where lswms_checkdetail.sku = lswms_inventory.sku and lswms_checkdetail.tray = lswms_inventory.tray and "
				+ " lswms_checkdetail.box = lswms_inventory.box and lswms_checkdetail.location = lswms_inventory.box location and  "
				+ " lswms_checkdetail.lot = lswms_inventory.lot"  )  ;
		Integer countCheckdetail = checkdetailDao.selectCount(checkdetailQuery) ;
		if(countCheckdetail>0) {
			return " 盘点的箱没有全部回到原位，无法取消";
		}
		
		UpdateWrapper<CheckEntity> updateCheck = new UpdateWrapper<CheckEntity>();
		updateCheck.eq("checkkey", checkkey);
		CheckEntity checkEntityUpdate = new CheckEntity();
		checkEntityUpdate.setStatus(CheckConstant.CHECK_STATUS_15);
		baseDao.update(checkEntityUpdate, updateCheck) ;
		
		UpdateWrapper<CheckdetailEntity> updateCheckdetail = new UpdateWrapper<CheckdetailEntity>();
		updateCheckdetail.eq("checkkey", checkkey);
		CheckdetailEntity checkdetailEntityUpdate = new CheckdetailEntity();
		checkdetailEntityUpdate.setStatus(CheckConstant.CHECK_STATUS_15);
		checkdetailDao.update(checkdetailEntityUpdate, updateCheckdetail) ;
		
		return null;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public String valideAndDelete(Long[] ids) {

		QueryWrapper<CheckEntity> checkQuery = new QueryWrapper<CheckEntity>();
		checkQuery.in("id", ids) ;
		List<CheckEntity> list = baseDao.selectList(checkQuery);
		
		List<String> checkkeyList = new ArrayList<String>();
		for (CheckEntity checkEntity : list) {
			Integer status = checkEntity.getStatus();
			if(status == CheckConstant.CHECK_STATUS_3) {
				return " 盘点任务已生成，无法删除" ;
			}
			if(status == CheckConstant.CHECK_STATUS_5) {
				return " 盘点正在盘点中，无法删除" ;
			}
			if(status == CheckConstant.CHECK_STATUS_7) {
				return " 盘点回库中，无法删除" ;
			}
			if(status == CheckConstant.CHECK_STATUS_9) {
				return " 盘点已过账，无法删除" ;
			}
			if(status == CheckConstant.CHECK_STATUS_15) {
				return " 盘点已取消，无法删除" ;
			}
			checkkeyList.add(checkEntity.getCheckkey());
		}
		
		UpdateWrapper<CheckdetailEntity> updateCheckdetail = new UpdateWrapper<CheckdetailEntity>();
		updateCheckdetail.in("checkkey",checkkeyList);
		checkdetailDao.delete(updateCheckdetail);
		
		baseDao.deleteBatchIds(Arrays.asList(ids)) ;
		
		return null;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public String valideAndUpdate(CheckDTO dto) {
		QueryWrapper<CheckEntity> checkQuery = new QueryWrapper<CheckEntity>();
		checkQuery.in("checkkey", dto.getCheckkey()) ;
		List<CheckEntity> list = baseDao.selectList(checkQuery);
		
		for (CheckEntity checkEntity : list) {
			Integer status = checkEntity.getStatus();
			if(status == CheckConstant.CHECK_STATUS_1) {
				return " 盘点明细已生成，无法修改" ;
			}
			if(status == CheckConstant.CHECK_STATUS_3) {
				return " 盘点任务已生成，无法修改" ;
			}
			if(status == CheckConstant.CHECK_STATUS_5) {
				return " 盘点正在盘点中，无法修改" ;
			}
			if(status == CheckConstant.CHECK_STATUS_7) {
				return " 盘点回库中，无法修改" ;
			}
			if(status == CheckConstant.CHECK_STATUS_9) {
				return " 盘点已过账，无法修改" ;
			}
			if(status == CheckConstant.CHECK_STATUS_15) {
				return " 盘点已取消，无法修改" ;
			}
			if(status != CheckConstant.CHECK_STATUS_0) {
				return " 盘点不是新建状态，无法修改" ;
			}
		}
		update(dto);
		return null;
	}

}